package com.ysd.lis.service.lab.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.lab.LabInspectSalesman;
import com.ysd.lis.mapper.lab.LabInspectSalesmanMapper;
import com.ysd.lis.request.lab.CompanyPageParam;
import com.ysd.lis.service.lab.LabInspectSalesmanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 送检销售人员 服务实现类
 * </p>
 *
 * @author WH
 * @since 2024-10-28
 */
@Service
public class LabInspectSalesmanServiceImpl extends ServiceImpl<LabInspectSalesmanMapper, LabInspectSalesman> implements LabInspectSalesmanService {

    @Autowired
    private LabInspectSalesmanMapper labInspectSalesmanMapper;
    @Override
    public Result findSalesmanPageList(CompanyPageParam pageParam) {
        Page<LabInspectSalesman> page = new Page<>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        LambdaQueryWrapper<LabInspectSalesman> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LabInspectSalesman::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(LabInspectSalesman::getCode, pageParam.getSeachValue())
                    .or().like(LabInspectSalesman::getName, pageParam.getSeachValue())
                    .or().like(LabInspectSalesman::getInputCode, pageParam.getSeachValue()));
        }
        lambdaQueryWrapper.orderByAsc(LabInspectSalesman::getSeq);
        Page<LabInspectSalesman> supplierPage = labInspectSalesmanMapper.selectPage(page, lambdaQueryWrapper);
        return Result.succ(1, "查询成功", supplierPage);
    }

    @Override
    public Result findSalesmanList(CompanyPageParam pageParam) {
        MPJLambdaWrapper<LabInspectSalesman> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.eq(LabInspectSalesman::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(LabInspectSalesman::getCode, pageParam.getSeachValue())
                    .or().like(LabInspectSalesman::getName, pageParam.getSeachValue())
                    .or().like(LabInspectSalesman::getInputCode, pageParam.getSeachValue()));
        }
        lambdaQueryWrapper.notExists("SELECT 1 FROM LAB_INSPECT_COMPANY_SALESMAN t2 WHERE t2.DEL_FLAG=0 and t.CODE = t2.CODE ");
        lambdaQueryWrapper.orderByAsc(LabInspectSalesman::getSeq);
        List<LabInspectSalesman> selectList = labInspectSalesmanMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "查询成功", selectList);
    }


    @Override
    public Result findSalesmansByPendSettle(CompanyPageParam pageParam) {
        MPJLambdaWrapper<LabInspectSalesman> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(LabInspectSalesman.class)
                .exists(" SELECT 1 FROM LAB_FINANCE_SETTLE T1 WHERE  t1.SALESMAN_CODE = t.code AND t1.SETTLE_STATUS =10  AND t1.del_flag = 0")
                .eq(LabInspectSalesman::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(LabInspectSalesman::getCode, pageParam.getSeachValue())
                    .or().like(LabInspectSalesman::getName, pageParam.getSeachValue())
                    .or().like(LabInspectSalesman::getInputCode, pageParam.getSeachValue()));
        }
        lambdaQueryWrapper.orderByAsc(LabInspectSalesman::getSeq);

        List<LabInspectSalesman> selectList = labInspectSalesmanMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "查询成功", selectList);
    }

    @Override
    public Result getSalesmanList(CompanyPageParam pageParam) {
        MPJLambdaWrapper<LabInspectSalesman> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(LabInspectSalesman.class)
                .eq(LabInspectSalesman::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(LabInspectSalesman::getCode, pageParam.getSeachValue())
                    .or().like(LabInspectSalesman::getName, pageParam.getSeachValue())
                    .or().like(LabInspectSalesman::getInputCode, pageParam.getSeachValue()));
        }
        lambdaQueryWrapper.orderByAsc(LabInspectSalesman::getSeq);

        List<LabInspectSalesman> selectList = labInspectSalesmanMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "查询成功", selectList);
    }

    @Override
    public Result addInspectSalesman(LabInspectSalesman labInspectSalesman) {
        int insert = labInspectSalesmanMapper.insert(labInspectSalesman);
        if (insert > 0) {
            return Result.succ(1, "添加成功", labInspectSalesman);
        } else {
            return Result.fail(400, "添加失败", "");
        }
    }

    @Override
    public Result editInspectCompany(LabInspectSalesman labInspectSalesman) {
        int updateById = labInspectSalesmanMapper.updateById(labInspectSalesman);
        if (updateById > 0) {
            return Result.succ(1, "提交成功", labInspectSalesman);
        } else {
            return Result.fail(400, "修改失败", "");
        }
    }

    @Override
    public Result batchDeleteSalesman(List<LabInspectSalesman> list) {

        List<String> ids = list.stream().map(LabInspectSalesman::getId).collect(Collectors.toList());

        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }
}
